﻿namespace IPMS.BLL
{
    using IPMS.DAL;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class Finance
    {
        private string _ChargeFrom;
        private string _Description;
        private string _InvoiceNumber;
        private string _ProjectCode;
        private DateTime _ReferenceDate;
        private string _ServiceCharge;
        private bool _IsDelete;

        public int GetProjectExpenseInfo(string projectCode, string invoiceNo)
        {
            SqlCommand sqlCmd = new SqlCommand();
            DataAccessLayer DAL = new DataAccessLayer();
            DAL.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@ProjectCode", SqlDbType.NVarChar, 30, ParameterDirection.Input, projectCode);
            DAL.AddParamToSQLCmd(sqlCmd, "@InvoiceNumber", SqlDbType.NVarChar, 30, ParameterDirection.Input, invoiceNo);
            DAL.AddParamToSQLCmd(sqlCmd, "@ReferenceDate", SqlDbType.DateTime, 8, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@ChargeFrom", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Description", SqlDbType.NVarChar, 0xff, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@ServiceCharge", SqlDbType.NVarChar, 13, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@IsDelete", SqlDbType.Bit, 1, ParameterDirection.Output, null);
            DAL.SetCommandType(sqlCmd, CommandType.StoredProcedure, "RetrieveProjectExpensesSp");
            DAL.ExecuteScalarCmd(sqlCmd);
            int resultValue = (int) sqlCmd.Parameters["@ReturnValue"].Value;
            this._ReferenceDate = (DateTime) sqlCmd.Parameters["@ReferenceDate"].Value;
            this._ChargeFrom = IPDataFormat.ValidString(sqlCmd.Parameters["@ChargeFrom"].Value.ToString());
            this._Description = IPDataFormat.ValidString(sqlCmd.Parameters["@Description"].Value.ToString());
            this._ServiceCharge = IPDataFormat.ValidString(sqlCmd.Parameters["@ServiceCharge"].Value.ToString());
            this._IsDelete = Convert.ToBoolean(sqlCmd.Parameters["@IsDelete"].Value);
            return resultValue;
        }

        public bool Update(bool IsDelete, string UpdateBy, string UpdateIP, string ProjectCode, string InvoiceNumber)
        {
            bool isSuccess = false;
            try
            {
                SqlCommand sqlCommand = new SqlCommand();
                DataAccessLayer DAL = new DataAccessLayer();

                string sqlString = "UPDATE [ProjectExpenses] SET IsDelete = @IsDelete,UpdateBy = @UpdateBy,UpdateDate = GETDATE(), UpdateIP = @UpdateIP WHERE (([ProjectCode] = @Original_ProjectCode) AND ( InvoiceNumber = @Original_InvoiceNumber )) ";

                DAL.SetCommandType(sqlCommand, CommandType.Text, sqlString);
                DAL.AddParamToSQLCmd(sqlCommand, "@IsDelete", SqlDbType.Bit, 1, ParameterDirection.Input, IsDelete);
                DAL.AddParamToSQLCmd(sqlCommand, "@UpdateBy", SqlDbType.NVarChar, 60, ParameterDirection.Input, UpdateBy);
                DAL.AddParamToSQLCmd(sqlCommand, "@UpdateIP", SqlDbType.NVarChar, 50, ParameterDirection.Input, UpdateIP);
                DAL.AddParamToSQLCmd(sqlCommand, "@Original_ProjectCode", SqlDbType.NVarChar, 60, ParameterDirection.Input, ProjectCode);
                DAL.AddParamToSQLCmd(sqlCommand, "@Original_InvoiceNumber", SqlDbType.NVarChar, 60, ParameterDirection.Input, InvoiceNumber);
                DAL.ExecuteScalarCmd(sqlCommand);

                isSuccess = true;
            }
            catch { }

            return isSuccess;
        }

        public bool Delete(string ProjectCode, string InvoiceNumber)
        {
            bool isSuccess = false;
            try
            {
                SqlCommand sqlCommand = new SqlCommand();
                DataAccessLayer DAL = new DataAccessLayer();

                string sqlString = "DELETE FROM [ProjectExpenses] WHERE (([ProjectCode] = @Original_ProjectCode) AND ( InvoiceNumber = @Original_InvoiceNumber )) ";

                DAL.SetCommandType(sqlCommand, CommandType.Text, sqlString);
                DAL.AddParamToSQLCmd(sqlCommand, "@Original_ProjectCode", SqlDbType.NVarChar, 60, ParameterDirection.Input, ProjectCode);
                DAL.AddParamToSQLCmd(sqlCommand, "@Original_InvoiceNumber", SqlDbType.NVarChar, 60, ParameterDirection.Input, InvoiceNumber);
                DAL.ExecuteScalarCmd(sqlCommand);

                isSuccess = true;
            }
            catch { }

            return isSuccess;
        }

        public string ChargeFrom
        {
            get
            {
                return this._ChargeFrom;
            }
        }

        public string Description
        {
            get
            {
                return this._Description;
            }
        }

        public string InvoiceNo
        {
            get
            {
                return this._InvoiceNumber;
            }
        }

        public string ProjectCode
        {
            get
            {
                return this._ProjectCode;
            }
        }

        public DateTime ReferenceDate
        {
            get
            {
                return this._ReferenceDate;
            }
        }

        public string ServiceCharge
        {
            get
            {
                return this._ServiceCharge;
            }
        }

        public bool IsDelete
        {
            get
            {
                return this._IsDelete;
            }
        }
    }
}

